/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.bean.LicenseInfo;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.CooperBillItem;
import com.minierp.model.Establishment;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class GatherBillListForAccountExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"GatherBillListForAccount.xls\"");
		HSSFFont titltFont = workbook.createFont();
		titltFont.setFontHeightInPoints(16);
		titltFont.setBoldweight(700);
		HSSFFont boldFont = workbook.createFont();
		boldFont.setBoldweight(700);
		HSSFFont normalFont = workbook.createFont();
		HSSFCellStyle titleCellStyle = workbook.createCellStyle();
		titleCellStyle.setFont(titltFont);
		titleCellStyle.setAlignment(2);
		titleCellStyle.setVerticalAlignment(1);
		HSSFCellStyle boldCellStyle = workbook.createCellStyle();
		boldCellStyle.setFont(boldFont);
		boldCellStyle.setBorderBottom(1);
		boldCellStyle.setBorderTop(1);
		boldCellStyle.setBorderLeft(1);
		boldCellStyle.setBorderRight(1);
		HSSFCellStyle norCellStyle = workbook.createCellStyle();
		norCellStyle.setBorderBottom(1);
		norCellStyle.setBorderTop(1);
		norCellStyle.setBorderLeft(1);
		norCellStyle.setBorderRight(1);
		Establishment est = (Establishment) model.get("est");
		LicenseInfo lif = EncCommonHelper.getLicenseInfo(request);
		HSSFSheet sheet = workbook.createSheet("list");
		byte colNum = 11;
		HSSFRow r = sheet.createRow(0);
		r.setHeight(600);
		HSSFCell c = r.createCell(0);

		for (int ts = 1; ts < colNum; ++ts) {
			r.createCell((short) ts);
		}

		c.setCellStyle(titleCellStyle);
		c.setCellValue(new HSSFRichTextString(lif.getEnterpriseName()
				+ " 收货对帐单"));
		sheet.addMergedRegion(new Region(0, 0, 0, (short) (colNum - 1)));
		r = sheet.createRow(1);
		c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		HSSFRichTextString arg25 = new HSSFRichTextString("外协商："
				+ est.getEstName());
		arg25.applyFont(0, 4, boldFont);
		arg25.applyFont(4, arg25.length(), normalFont);
		c.setCellValue(arg25);
		sheet.addMergedRegion(new Region(1, 0, 1, 2));
		c = r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		arg25 = new HSSFRichTextString("起止日期："
				+ model.get("start_date").toString() + " 至 "
				+ model.get("end_date").toString());
		arg25.applyFont(0, 5, boldFont);
		arg25.applyFont(5, arg25.length(), normalFont);
		c.setCellValue(arg25);
		sheet.addMergedRegion(new Region(1, 3, 1, 5));
		c = r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		r.createCell(9);
		r.createCell(10);
		arg25 = new HSSFRichTextString("收货金额：" + model.get("costTotal"));
		arg25.applyFont(0, 5, boldFont);
		arg25.applyFont(5, arg25.length(), normalFont);
		c.setCellValue(arg25);
		sheet.addMergedRegion(new Region(1, 6, 1, 10));
		r = sheet.createRow(2);
		c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		arg25 = new HSSFRichTextString("地址：" + est.getAddress());
		arg25.applyFont(0, 3, boldFont);
		arg25.applyFont(3, arg25.length(), normalFont);
		c.setCellValue(arg25);
		sheet.addMergedRegion(new Region(2, 0, 2, 2));
		c = r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		arg25 = new HSSFRichTextString("联系人：" + est.getLinkman());
		arg25.applyFont(0, 4, boldFont);
		arg25.applyFont(4, arg25.length(), normalFont);
		c.setCellValue(arg25);
		sheet.addMergedRegion(new Region(2, 3, 2, 5));
		c = r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		r.createCell(9);
		r.createCell(10);
		arg25 = new HSSFRichTextString("电话：" + est.getLinkmanPhone());
		arg25.applyFont(0, 3, boldFont);
		arg25.applyFont(3, arg25.length(), normalFont);
		c.setCellValue(arg25);
		sheet.addMergedRegion(new Region(2, 6, 2, 10));
		r = sheet.createRow(3);
		c = r.createCell(0);

		for (int list = 1; list < colNum; ++list) {
			r.createCell((short) list);
		}

		c.setCellValue(new HSSFRichTextString(""));
		sheet.addMergedRegion(new Region(3, 0, 3, (short) (colNum - 1)));
		r = sheet.createRow(4);
		c = r.createCell(0);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("单号"));
		c = r.createCell(1);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("日期"));
		c = r.createCell(2);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("项目类型"));
		c = r.createCell(3);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("内容"));
		c = r.createCell(4);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("材质"));
		c = r.createCell(5);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("规格"));
		c = r.createCell(6);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("单位"));
		c = r.createCell(7);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("数量"));
		c = r.createCell(8);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("单价"));
		c = r.createCell(9);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("金额"));
		c = r.createCell(10);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("制作要求"));
		List arg26 = (List) model.get("list");
		List mtList = (List) model.get("mtList");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

		for (int os = 0; os < arg26.size(); ++os) {
			Object[] objs = (Object[]) arg26.get(os);
			CooperBillItem item = (CooperBillItem) objs[0];
			String spec = item.getSpecification();
			if (spec == null) {
				spec = "";
			}

			String unit;
			if (item.getPriceType().equals("AREA")) {
				spec = spec + " x " + item.getAmount();
				unit = "平方米";
			} else if (item.getPriceType().equals("LENGTH")) {
				spec = spec + " x " + item.getAmount();
				if (item.getLengType().equals("M")) {
					unit = "米";
				} else {
					unit = "公分";
				}
			} else {
				unit = item.getUnit();
			}

			r = sheet.createRow(os + 5);
			c = r.createCell(0);
			c.setCellStyle(norCellStyle);
			c.setCellValue(new HSSFRichTextString((String) objs[1]));
			c = r.createCell(1);
			c.setCellStyle(norCellStyle);
			c.setCellValue(new HSSFRichTextString(sf.format((Date) objs[2])));
			c = r.createCell(2);
			c.setCellStyle(norCellStyle);
			c.setCellValue(new HSSFRichTextString(EncCommonHelper
					.getSimpleObjectName(mtList, item.getMakeTypeId())));
			c = r.createCell(3);
			c.setCellStyle(norCellStyle);
			c.setCellValue(new HSSFRichTextString(item.getFinishedContent()));
			c = r.createCell(4);
			c.setCellStyle(norCellStyle);
			c.setCellValue(new HSSFRichTextString(item.getProName()));
			c = r.createCell(5);
			c.setCellStyle(norCellStyle);
			c.setCellValue(new HSSFRichTextString(spec));
			c = r.createCell(6);
			c.setCellStyle(norCellStyle);
			c.setCellValue(new HSSFRichTextString(unit));
			c = r.createCell(7);
			c.setCellStyle(norCellStyle);
			c.setCellValue(((BigDecimal) objs[3]).doubleValue());
			c = r.createCell(8);
			c.setCellStyle(norCellStyle);
			c.setCellValue(item.getUnitPrice().doubleValue());
			c = r.createCell(9);
			c.setCellStyle(norCellStyle);
			c.setCellValue(((BigDecimal) objs[4]).doubleValue());
			c = r.createCell(10);
			c.setCellStyle(norCellStyle);
			c.setCellValue(new HSSFRichTextString(item.getMakeRequire()));
		}

		sheet.setColumnWidth(0, 5000);
		sheet.setColumnWidth(1, 3200);
		ServletOutputStream arg27 = response.getOutputStream();
		workbook.write(arg27);
		arg27.flush();
		arg27.close();
	}
}